// litvalues.cpp
// Customizer_Base
// Protea Customizer/Customizer Pro / Business Logic for managing literal values
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// Please send correspondence to:
//
// theproteaproject@gmail.com
//

#include "base_setup.h" 
//////////////////////////////////////////////////////////////////////////////
//
// LITERAL_VALUES Subtransactions
//
////////////////////////////////////////////////////////////////////////////// 
//@ SEC 3
//@ CHA Literal Value Verify
//@ COD LiteralValueVerify
//@ TAB LITERAL_VALUE Read
//@ GEN Use LiteralValueVerify to verify literal value record
void LiteralValueVerify( XCBuffer* buf, XCTransactionVars* vars, verify_type verify_mode, XCBuffer* orig ) 
{
	TRANS_INFUNC( LiteralValueVerify );
		bool result = LiteralValueRetrieve( buf, vars ); 		
		//@ If verifying an ADD transaction,
		if (verify_mode == VERIFY_ADD) 
		{
		//@ GRS
			if (result)
			{
				vars->Errors->Add( ERR_ALREADY_EXISTS, SEV_CRITICAL, FLD_LANG_ID, 1, ERR_LANG_ID );
			}
		//@ GRE
		} 
		else 
		{
		//@ GRS
			if (!result)
			{
				vars->Errors->Add( ERR_DOESNT_EXIST, SEV_CRITICAL, FLD_LANG_ID, 1, ERR_LANG_ID );
			}
		//@ GRE
		}; 	
	TRANS_OUTFUNC();
}; 

//@ SEC 3
//@ CHA Literal Value Retrieve
//@ COD LiteralValueRetrieve
//@ TAB LITERAL_VALUES Read
//@ GEN Use Literal Value Retrieve to get record
bool LiteralValueRetrieve( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( LiteralValueRetrieve );
		bool result;
		XCDBStatement* stmt = NULL;
		XCSQLGen* query = vars->query;
		try 
		{
			query->MakeFull( SQL_STMT_SELECT, buf );
			stmt = vars->dbtxn->ExecuteSQL( query );
			result = (!stmt->Eof());
		}
		catch( ... ) 
		{
			App->CleanupObjects( 1, &stmt );
			throw;
		};
		App->CleanupObjects( 1, &stmt );
	TRANS_OUTFUNCRET( result );
};

//@ SEC 3
//@ CHA Literal Value Add
//@ COD LiteralValueAdd
//@ TAB LITERAL_VALUES Write
//@ GEN Use LiteralValueAdd to add a literal value record
void LiteralValueAdd( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( LiteralValueAdd );
		//@ Add the literal record
		vars->query->MakeFull( SQL_STMT_INSERT, buf );
		vars->dbtxn->ExecuteSQL( vars->query, SQL_NO_RESULT );
	TRANS_OUTFUNC();
}; 

//@ SEC 3
//@ CHA Literal Value Delete
//@ COD LiteralValueDelete
//@ TAB LITERAL_VALUES Write
//@ GEN Use LiteralValueDelete to remove a literal value record
void LiteralValueDelete( XCBuffer* buf, XCTransactionVars* vars ) 
{
	TRANS_INFUNC( LiteralValueDelete );
		//@ Add the literal record
		vars->query->MakeFull( SQL_STMT_DELETE, buf );
		vars->dbtxn->ExecuteSQL( vars->query, SQL_NO_RESULT );
	TRANS_OUTFUNC();
}; 

//@ SEC 3
//@ CHA Literal Value Change
//@ COD LiteralValueChange
//@ TAB LITERAL_VALUES Write
//@ GEN Use LiteralValueChange to alter a literal value record
void LiteralValueChange( XCBuffer* input, XCTransactionVars* vars, XCBuffer* orig ) 
{
	XCBuffer* Literal_Defn = NULL;
	XCField* Literal_Value = NULL;
	TRANS_INFUNC( LiteralValueChange );
		//@ Modify the literal length on the LITERAL_DEFN record
		Literal_Defn = App->CreateBuffer( FMT_LITERAL_DEFN );
		Literal_Defn->MoveCorresponding( input, vars->Errors );
		Literal_Value = input->GetField( FLD_LITERAL );
		Literal_Defn->GetField( FLD_LITERAL_LENGTH )->SetLong( strlen( Literal_Value->GetOffset() ) );

		//@ Issue update statement
		vars->query->MakeFull( SQL_STMT_UPDATE, input, orig );
		vars->dbtxn->ExecuteSQL( vars->query, SQL_NO_RESULT );
	TRANS_OUTFUNC();
	App->CleanupObjects( 1, &Literal_Defn );
};
